using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using NCI.EasyObjects;
using Microsoft.Practices.EnterpriseLibrary;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data.Common;
using System.IO;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;

public class FileUpload
{
    public static bool checkFileExtension(string fileName)
    {
        bool bCheckExtension = false;
        String[] allowedExtensions = { ".doc", ".pdf", ".zip", ".rar", ".xls"};

        string sFileExten = Path.GetExtension(fileName);
        fileName = sFileExten.ToLower();

        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (fileName == allowedExtensions[i])
            {
                bCheckExtension = true;
            }
        }
        return bCheckExtension;
    }

    public static string GetFileExtension(HttpPostedFile FilePosted)
    {
        string sFileName = "";
        HttpPostedFile upPhoto = FilePosted;
        sFileName = FilePosted.FileName;
        sFileName = Path.GetFileName(sFileName);
        sFileName = Path.GetExtension(sFileName);
        return sFileName;
    }

    public static string GetFileName(HttpPostedFile FilePosted)
    {
        string sFileName = "";
        HttpPostedFile upPhoto = FilePosted;
        sFileName = FilePosted.FileName;
        sFileName = Path.GetFileName(sFileName);
        return sFileName;
    }

    public static string UploadImageToServer(string MapPath, HttpPostedFile Input, string FileExtension, string SavingFolder)
    {
        string sUploadImageFolder = SavingFolder;
        string sCurrentDatetime = "";
        string sFolder = "";
        string sFileName = "";
        string sFilePath = "";
        sCurrentDatetime = DateTime.Now.Ticks.ToString();
        sFolder = MapPath;

        //sFileName = Input.FileName;// ConvertFileName(Input.FileName);
        sFileName = sCurrentDatetime + FileExtension;
        
        sFolder = MapPath;
        sFolder = sFolder + sUploadImageFolder;

        if (!Directory.Exists(sFolder))
        {
            Directory.CreateDirectory(sFolder);
        }
        sFilePath = sFolder + sFileName;
        try {
            Input.SaveAs(sFilePath);
        }
        catch {
            sFilePath = sFolder + sFileName;
            Input.SaveAs(sFilePath);
        }
        
        return sFileName;

    }
    public static string DeleteImageFromServer(string MapPath, string sDelFolder, string FileName)
    {
        string delResult = FileName;
        try
        {
            File.Delete(MapPath + sDelFolder + Config.SLASH + FileName);
        }
        catch
        {
            delResult = "";
        }
        return delResult;
    }
    public static bool checkExtension(string FileName)
    {
        bool bCheckExtension = false;
        String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp", ".ico", ".pcx", ".psd", ".tga", ".tif", ".doc", ".pdf", ".xls",".xlsx", ".txt", ".docx", ".zip", ".rar" };

        string sFileExten = Path.GetExtension(FileName);
        FileName = sFileExten.ToLower();

        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (FileName == allowedExtensions[i])
            {
                bCheckExtension = true;
            }
        }
        return bCheckExtension;
    }
    public static bool checkImageExtension2(string ImageName)
    {
        bool bCheckExtension = false;
        String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp"};

        string sFileExten = Path.GetExtension(ImageName);
        ImageName = sFileExten.ToLower();

        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (ImageName == allowedExtensions[i])
            {
                bCheckExtension = true;
            }
        }
        return bCheckExtension;
    }

    public static bool checkFileUploadExtension(string FileUpload)
    {
        bool bCheckExtension = false;
        String[] allowedExtensions = { ".doc", ".pdf", ".xls", ".txt", "docx", "zip", "rar" };

        string sFileExten = Path.GetExtension(FileUpload);
        FileUpload = sFileExten.ToLower();

        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (FileUpload == allowedExtensions[i])
            {
                bCheckExtension = true;
            }
        }
        return bCheckExtension;
    }

    public static string ConvertFileName(string FileName)
    {
        string cFileName = FileName;
        cFileName = cFileName.Replace("/", "");
        cFileName = cFileName.Replace(" ", "");
        cFileName = cFileName.Replace(":", "");
        cFileName = cFileName.Replace(Path.GetExtension(FileName), "");
        return cFileName;
    }

    public static bool checkFileSize(HttpPostedFile FilePost, int FileSize)
    {
        int nLimitSize = 0;
        nLimitSize = 1024000 * FileSize;

        bool bCheckImage = false;
        HttpPostedFile upPhoto = FilePost;

        if (upPhoto.ContentLength > nLimitSize) // OverFlow
            bCheckImage = false;
        else
            bCheckImage = true;
        return bCheckImage; ;
    }
}

